import sys

from PyQt5.QtCore import Qt, QPoint, pyqtSignal
from PyQt5.QtGui import QMouseEvent, QCursor, QPainter, QBrush, QPixmap
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QLabel

import pymysql

from main_win.user import Ui_userwin
from utils.CustomMessageBox import MessageBox


# paint a round img
class CircleImage(QWidget):

    def __init__(self, parent=None):
        super(CircleImage, self).__init__(parent)
        self.resize(100, 100)
        self.circle_image = None

    def set_image(self, image):
        # set the img to paint
        self.circle_image = image
        self.update()

    # rewrite the paint event
    def paintEvent(self, event):
        super(CircleImage, self).paintEvent(event)
        painter = QPainter(self)
        # set anti aliasing
        painter.setRenderHint(QPainter.Antialiasing, True)
        pen = Qt.NoPen
        # set no border
        painter.setPen(pen)
        brush = QBrush(self.circle_image)
        # set what to paint
        painter.setBrush(brush)
        painter.drawRoundedRect(self.rect(), self.width() / 2, self.height() / 2)


class user_window(QMainWindow, Ui_userwin):
    # connect to mysql
    mysql = pymysql.connect(
        host='127.0.0.1',
        user='root',
        password='123456',
        port=3306,
        db='imagerecognition',
        autocommit=True,
    )
    cursor = mysql.cursor()
    sql = "SELECT * FROM users WHERE id=%s"

    def __init__(self, parent=None):
        super(user_window, self).__init__(parent)
        self.setupUi(self)
        # user id according to main window
        self.userid = 1
        # no border and transparent
        self.m_flag = False
        self.setAttribute(Qt.WA_TranslucentBackground)
        self.setWindowFlags(Qt.FramelessWindowHint)
        self.closeButton.clicked.connect(self.close)
        self.minButton.clicked.connect(self.showMinimized)

        # user avatar a round img
        self.imgwidget = CircleImage(self.widget)
        self.img = QPixmap('./userimg/0.png')

    # load user data if id changed
    def load(self):
        # avatar
        self.img = QPixmap('./userimg/0.png')
        self.imgwidget.set_image(self.img.scaled(self.widget.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation))
        # name
        self.cursor.execute(self.sql, self.userid)
        a = self.cursor.fetchone()
        self.idnum.setText('ID:' + str(a[0]))
        self.username.setText('Name:' + a[1])

    # rewrite events according to mouse to realize window moving
    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.m_flag = True
            self.m_Position = event.globalPos() - self.pos()
            event.accept()

    def mouseMoveEvent(self, QMouseEvent):
        if Qt.LeftButton and self.m_flag:
            self.move(QMouseEvent.globalPos() - self.m_Position)
            QMouseEvent.accept()

    def mouseReleaseEvent(self, QMouseEvent):
        self.m_flag = False
        self.setCursor(QCursor(Qt.ArrowCursor))


if __name__ == '__main__':
    app = QApplication(sys.argv)
    a = user_window(1)
    a.show()
    sys.exit(app.exec_())
